//
//  MainTabBarController.m
//  TaskManager
//
//  Created by Sun on 2019/3/1.
//  Copyright © 2019 Sun. All rights reserved.
//

#import "MainTabBarController.h"
#import "HomeVC.h"
#import "TranManagerVC.h"
#import "MineVC.h"
#import "OperationBillManagerMainVC.h"
#import "GrabOrderMainVC.h"
#import "AbnormalReportMainVC.h"

@interface MainTabBarController ()<UITabBarControllerDelegate>

@end

@implementation MainTabBarController

+ (instancetype)instance{
    UIViewController *vc = [[[UIApplication sharedApplication] delegate]window].rootViewController;
    if ([vc isKindOfClass:[MainTabBarController class]]) {
        return (MainTabBarController *)vc;
    }else{
        return nil;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = Color_backgroundColor;
    
    if ([User userIsDriver]) {
        //单车
//        OperationBillManagerMainVC *vc = [OperationBillManagerMainVC new];
//        [self createTabBarWithChildVC:vc andItem:@"操作单" normalImage:@"tab_goods" selectedImage:@"tab_goods_sel"];
        HomeVC *homeVC = [HomeVC new];
        [self createTabBarWithChildVC:homeVC andItem:@"首页" normalImage:@"tab_goods" selectedImage:@"tab_goods_sel"];
        GrabOrderMainVC *grabOrderVC = [GrabOrderMainVC new];
        [self createTabBarWithChildVC:grabOrderVC andItem:@"抢单" normalImage:@"tab_rob" selectedImage:@"tab_rob"];
        AbnormalReportMainVC *abnormalReportVC = [AbnormalReportMainVC new];
        abnormalReportVC.type = 1;
        [self createTabBarWithChildVC:abnormalReportVC andItem:@"状态上报" normalImage:@"tab_abnormal" selectedImage:@"tab_abnormal"];
        TranManagerVC *scanVC = [TranManagerVC new];
        [self createTabBarWithChildVC:scanVC andItem:@"运输管理" normalImage:@"tab_waybill" selectedImage:@"tab_waybill_sel"];
        MineVC *mineVC = [MineVC new];
        [self createTabBarWithChildVC:mineVC andItem:@"我的" normalImage:@"tab_my" selectedImage:@"tab_my_sel"];
    }else {
        //多车
//        OperationBillManagerMainVC *vc = [OperationBillManagerMainVC new];
//        [self createTabBarWithChildVC:vc andItem:@"操作单" normalImage:@"tab_goods" selectedImage:@"tab_goods_sel"];
        HomeVC *homeVC = [HomeVC new];
        [self createTabBarWithChildVC:homeVC andItem:@"首页" normalImage:@"tab_goods" selectedImage:@"tab_goods_sel"];
        GrabOrderMainVC *grabOrderVC = [GrabOrderMainVC new];
        [self createTabBarWithChildVC:grabOrderVC andItem:@"抢单" normalImage:@"tab_rob" selectedImage:@"tab_rob"];
//        AbnormalReportMainVC *abnormalReportVC = [AbnormalReportMainVC new];
//        [self createTabBarWithChildVC:abnormalReportVC andItem:@"状态上报" normalImage:@"tab_abnormal" selectedImage:@"tab_abnormal"];
        TranManagerVC *scanVC = [TranManagerVC new];
        [self createTabBarWithChildVC:scanVC andItem:@"运输管理" normalImage:@"tab_waybill" selectedImage:@"tab_waybill_sel"];
        MineVC *mineVC = [MineVC new];
        [self createTabBarWithChildVC:mineVC andItem:@"我的" normalImage:@"tab_my" selectedImage:@"tab_my_sel"];
    }
    self.delegate = self;
    self.selectedIndex = 0;
    [[UITabBar appearance] setBarTintColor:[UIColor whiteColor]];
    [UITabBar appearance].translucent = NO;
}

-(void)setIdx:(NSUInteger)idx {
    _idx = idx;
    self.selectedIndex = idx;
}

/**
 *  自定义底部tabbar按方法
 *
 *  @param title       tabbar按钮的标题
 *  @param normalImg   未选择时候的图标
 *  @param selectedImg 选中时候的图标
 */
-(void)createTabBarWithChildVC:(UIViewController *)vc andItem:(NSString *)title normalImage:(NSString *)normalImg selectedImage:(NSString *)selectedImg {
    MainNavigationController *nav = [[MainNavigationController alloc] initWithRootViewController:vc];
//    NSDictionary *navTitleDic = [NSDictionary dictionaryWithObjectsAndKeys:HexColor(0xffffff),NSForegroundColorAttributeName,[UIFont systemFontOfSize:18],NSFontAttributeName, nil];
//    [vc.navigationController.navigationBar setTitleTextAttributes:navTitleDic];
    UIImage *myImage = [UIImage imageNamed:normalImg];
    myImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.image = myImage;
    
    UIImage *mySelectedImage = [UIImage imageNamed:selectedImg];
    mySelectedImage = [mySelectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.selectedImage = mySelectedImage;
//    [vc.tabBarItem setImageInsets:UIEdgeInsetsMake(-2, 2, 2, 0)];
    vc.tabBarItem.title = title;
    NSDictionary *titleSelectedDic = [NSDictionary dictionaryWithObjectsAndKeys:HexColor(0x3D3E5A),NSForegroundColorAttributeName,[UIFont systemFontOfSize:10],NSFontAttributeName, nil];
    NSDictionary *titleNormalDic = [NSDictionary dictionaryWithObjectsAndKeys:HexColor(0x9E9FB5),NSForegroundColorAttributeName,[UIFont systemFontOfSize:10],NSFontAttributeName, nil];
    [vc.tabBarItem setTitleTextAttributes:titleNormalDic forState:UIControlStateNormal];
    [vc.tabBarItem setTitleTextAttributes:titleSelectedDic forState:UIControlStateSelected];
    [self addChildViewController:nav];
}

- (void)dealloc {
    NSLog(@"---- %@ ---- dealloc ----",self.class);
}

@end
